<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>style.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_style.js.html">style.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilties for working with the styles of DOM nodes, and
<a name="line17"></a> * related to rich text editing.
<a name="line18"></a> *
<a name="line19"></a> * Many of these are not general enough to go into goog.style, and use
<a name="line20"></a> * constructs (like &quot;isContainer&quot;) that only really make sense inside
<a name="line21"></a> * of an HTML editor.
<a name="line22"></a> *
<a name="line23"></a> * The API has been optimized for iterating over large, irregular DOM
<a name="line24"></a> * structures (with lots of text nodes), and so the API tends to be a bit
<a name="line25"></a> * more permissive than the goog.style API should be. For example,
<a name="line26"></a> * goog.style.getComputedStyle will throw an exception if you give it a
<a name="line27"></a> * text node.
<a name="line28"></a> *
<a name="line29"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line30"></a> */
<a name="line31"></a>
<a name="line32"></a>goog.provide(&#39;goog.editor.style&#39;);
<a name="line33"></a>
<a name="line34"></a>goog.require(&#39;goog.dom&#39;);
<a name="line35"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line36"></a>goog.require(&#39;goog.editor.BrowserFeature&#39;);
<a name="line37"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line38"></a>goog.require(&#39;goog.object&#39;);
<a name="line39"></a>goog.require(&#39;goog.style&#39;);
<a name="line40"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Gets the computed or cascaded style.
<a name="line45"></a> *
<a name="line46"></a> * This is different than goog.style.getStyle_ because it returns null
<a name="line47"></a> * for text nodes (instead of throwing an exception), and never reads
<a name="line48"></a> * inline style. These two functions may need to be reconciled.
<a name="line49"></a> *
<a name="line50"></a> * @param {Node} node Node to get style of.
<a name="line51"></a> * @param {string} stylePropertyName Property to get (must be camelCase,
<a name="line52"></a> *     not css-style).
<a name="line53"></a> * @return {?string} Style value, or null if this is not an element node.
<a name="line54"></a> * @private
<a name="line55"></a> */
<a name="line56"></a>goog.editor.style.getComputedOrCascadedStyle_ = function(
<a name="line57"></a>    node, stylePropertyName) {
<a name="line58"></a>  if (node.nodeType != goog.dom.NodeType.ELEMENT) {
<a name="line59"></a>    // Only element nodes have style.
<a name="line60"></a>    return null;
<a name="line61"></a>  }
<a name="line62"></a>  return goog.userAgent.IE ?
<a name="line63"></a>      goog.style.getCascadedStyle(/** @type {Element} */ (node),
<a name="line64"></a>          stylePropertyName) :
<a name="line65"></a>      goog.style.getComputedStyle(/** @type {Element} */ (node),
<a name="line66"></a>          stylePropertyName);
<a name="line67"></a>};
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>/**
<a name="line71"></a> * Checks whether the given element inherits display: block.
<a name="line72"></a> * @param {Node} node The Node to check.
<a name="line73"></a> * @return {boolean} Whether the element inherits CSS display: block.
<a name="line74"></a> */
<a name="line75"></a>goog.editor.style.isDisplayBlock = function(node) {
<a name="line76"></a>  return goog.editor.style.getComputedOrCascadedStyle_(
<a name="line77"></a>      node, &#39;display&#39;) == &#39;block&#39;;
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Returns true if the element is a container of other non-inline HTML
<a name="line83"></a> * Note that span, strong and em tags, being inline can only contain
<a name="line84"></a> * other inline elements and are thus, not containers. Containers are elements
<a name="line85"></a> * that should not be broken up when wrapping selections with a node of an
<a name="line86"></a> * inline block styling.
<a name="line87"></a> * @param {Node} element The element to check.
<a name="line88"></a> * @return {boolean} Whether the element is a container.
<a name="line89"></a> */
<a name="line90"></a>goog.editor.style.isContainer = function(element) {
<a name="line91"></a>  var nodeName = element &amp;&amp; element.nodeName.toLowerCase();
<a name="line92"></a>  return !!(element &amp;&amp;
<a name="line93"></a>      (goog.editor.style.isDisplayBlock(element) ||
<a name="line94"></a>          nodeName == &#39;td&#39; ||
<a name="line95"></a>          nodeName == &#39;table&#39; ||
<a name="line96"></a>          nodeName == &#39;li&#39;));
<a name="line97"></a>};
<a name="line98"></a>
<a name="line99"></a>
<a name="line100"></a>/**
<a name="line101"></a> * Return the first ancestor of this node that is a container, inclusive.
<a name="line102"></a> * @see isContainer
<a name="line103"></a> * @param {Node} node Node to find the container of.
<a name="line104"></a> * @return {Element} The element which contains node.
<a name="line105"></a> */
<a name="line106"></a>goog.editor.style.getContainer = function(node) {
<a name="line107"></a>  // We assume that every node must have a container.
<a name="line108"></a>  return /** @type {Element} */ (
<a name="line109"></a>      goog.dom.getAncestor(node, goog.editor.style.isContainer, true));
<a name="line110"></a>};
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Set of input types that should be kept selectable even when their ancestors
<a name="line115"></a> * are made unselectable.
<a name="line116"></a> * @type {Object}
<a name="line117"></a> * @private
<a name="line118"></a> */
<a name="line119"></a>goog.editor.style.SELECTABLE_INPUT_TYPES_ = goog.object.createSet(
<a name="line120"></a>    &#39;text&#39;, &#39;file&#39;, &#39;url&#39;);
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Prevent the default action on mousedown events.
<a name="line125"></a> * @param {goog.events.Event} e The mouse down event.
<a name="line126"></a> * @private
<a name="line127"></a> */
<a name="line128"></a>goog.editor.style.cancelMouseDownHelper_ = function(e) {
<a name="line129"></a>  var targetTagName = e.target.tagName;
<a name="line130"></a>  if (targetTagName != goog.dom.TagName.TEXTAREA &amp;&amp;
<a name="line131"></a>      targetTagName != goog.dom.TagName.INPUT) {
<a name="line132"></a>    e.preventDefault();
<a name="line133"></a>  }
<a name="line134"></a>};
<a name="line135"></a>
<a name="line136"></a>
<a name="line137"></a>/**
<a name="line138"></a> * Makes the given element unselectable, as well as all of its children, except
<a name="line139"></a> * for text areas, text, file and url inputs.
<a name="line140"></a> * @param {Element} element The element to make unselectable.
<a name="line141"></a> * @param {goog.events.EventHandler} eventHandler An EventHandler to register
<a name="line142"></a> *     the event with. Assumes when the node is destroyed, the eventHandler&#39;s
<a name="line143"></a> *     listeners are destroyed as well.
<a name="line144"></a> */
<a name="line145"></a>goog.editor.style.makeUnselectable = function(element, eventHandler) {
<a name="line146"></a>  if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
<a name="line147"></a>    // The mousing down on a node should not blur the focused node.
<a name="line148"></a>    // This is consistent with how IE works.
<a name="line149"></a>    // TODO: Consider using just the mousedown handler and not the css property.
<a name="line150"></a>    eventHandler.listen(element, goog.events.EventType.MOUSEDOWN,
<a name="line151"></a>        goog.editor.style.cancelMouseDownHelper_, true);
<a name="line152"></a>  }
<a name="line153"></a>
<a name="line154"></a>  goog.style.setUnselectable(element, true);
<a name="line155"></a>
<a name="line156"></a>  // Make inputs and text areas selectable.
<a name="line157"></a>  var inputs = element.getElementsByTagName(goog.dom.TagName.INPUT);
<a name="line158"></a>  for (var i = 0, len = inputs.length; i &lt; len; i++) {
<a name="line159"></a>    var input = inputs[i];
<a name="line160"></a>    if (input.type in goog.editor.style.SELECTABLE_INPUT_TYPES_) {
<a name="line161"></a>      goog.editor.style.makeSelectable(input);
<a name="line162"></a>    }
<a name="line163"></a>  }
<a name="line164"></a>  goog.array.forEach(element.getElementsByTagName(goog.dom.TagName.TEXTAREA),
<a name="line165"></a>      goog.editor.style.makeSelectable);
<a name="line166"></a>};
<a name="line167"></a>
<a name="line168"></a>
<a name="line169"></a>/**
<a name="line170"></a> * Make the given element selectable.
<a name="line171"></a> *
<a name="line172"></a> * For IE this simply turns off the &quot;unselectable&quot; property.
<a name="line173"></a> *
<a name="line174"></a> * Under FF no descendent of an unselectable node can be selectable:
<a name="line175"></a> *
<a name="line176"></a> * https://bugzilla.mozilla.org/show_bug.cgi?id=203291
<a name="line177"></a> *
<a name="line178"></a> * So we make each ancestor of node selectable, while trying to preserve the
<a name="line179"></a> * unselectability of other nodes along that path
<a name="line180"></a> *
<a name="line181"></a> * This may cause certain text nodes which should be unselectable, to become
<a name="line182"></a> * selectable. For example:
<a name="line183"></a> *
<a name="line184"></a> * &lt;div id=div1 style=&quot;-moz-user-select: none&quot;&gt;
<a name="line185"></a> *   Text1
<a name="line186"></a> *   &lt;span id=span1&gt;Text2&lt;/span&gt;
<a name="line187"></a> * &lt;/div&gt;
<a name="line188"></a> *
<a name="line189"></a> * If we call makeSelectable on span1, then it will cause &quot;Text1&quot; to become
<a name="line190"></a> * selectable, since it had to make div1 selectable in order for span1 to be
<a name="line191"></a> * selectable.
<a name="line192"></a> *
<a name="line193"></a> * If &quot;Text1&quot; were enclosed within a &lt;p&gt; or &lt;span&gt;, then this problem would
<a name="line194"></a> * not arise.  Text nodes do not have styles, so its style can&#39;t be set to
<a name="line195"></a> * unselectable.
<a name="line196"></a> *
<a name="line197"></a> * @param {Element} element The element to make selectable.
<a name="line198"></a> */
<a name="line199"></a>goog.editor.style.makeSelectable = function(element) {
<a name="line200"></a>  goog.style.setUnselectable(element, false);
<a name="line201"></a>  if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
<a name="line202"></a>    // Go up ancestor chain, searching for nodes that are unselectable.
<a name="line203"></a>    // If such a node exists, mark it as selectable but mark its other children
<a name="line204"></a>    // as unselectable so the minimum set of nodes is changed.
<a name="line205"></a>    var child = element;
<a name="line206"></a>    var current = /** @type {Element} */ (element.parentNode);
<a name="line207"></a>    while (current &amp;&amp; current.tagName != goog.dom.TagName.HTML) {
<a name="line208"></a>      if (goog.style.isUnselectable(current)) {
<a name="line209"></a>        goog.style.setUnselectable(current, false, true);
<a name="line210"></a>
<a name="line211"></a>        for (var i = 0, len = current.childNodes.length; i &lt; len; i++) {
<a name="line212"></a>          var node = current.childNodes[i];
<a name="line213"></a>          if (node != child &amp;&amp; node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line214"></a>            goog.style.setUnselectable(current.childNodes[i], true);
<a name="line215"></a>          }
<a name="line216"></a>        }
<a name="line217"></a>      }
<a name="line218"></a>
<a name="line219"></a>      child = current;
<a name="line220"></a>      current = /** @type {Element} */ (current.parentNode);
<a name="line221"></a>    }
<a name="line222"></a>  }
<a name="line223"></a>};
</pre>


</body>
</html>
